package com.zhongen.oa.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongen.common.core.util.R;
import com.zhongen.common.log.annotation.SysLog;
import com.zhongen.common.security.annotation.Inner;
import com.zhongen.oa.entity.ProjectBasicInfoEntity;
import com.zhongen.oa.entity.vo.ProjectBasicInfoVo;
import com.zhongen.oa.service.ProjectBasicInfoService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目基本信息表
 *
 * @author Code Generator
 * @date 2024-01-15 12:07:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/projectBasicInfo")
@Tag(description = "projectBasicInfo", name = "项目基本信息表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProjectBasicInfoController {

	private final ProjectBasicInfoService projectBasicInfoService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param projectBasicInfo 项目基本信息表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('oa_projectBasicInfo_view')")
	public R getProjectBasicInfoPage(@ParameterObject Page page,
			@ParameterObject ProjectBasicInfoEntity projectBasicInfo) {
		return R.ok(projectBasicInfoService.page(page, projectBasicInfo));
	}

	/**
	 * 查询项目数量
	 * @param projectBasicInfo 查询项目数量
	 * @return
	 */
	@Operation(summary = "查询项目数量", description = "查询项目数量")
	@GetMapping("/projectBasicInfoNum")
	@PreAuthorize("@pms.hasPermission('oa_projectBasicInfo_view')")
	public R getProjectBasicInfoNumApp(@ParameterObject ProjectBasicInfoEntity projectBasicInfo) {
		return R.ok(projectBasicInfoService.getProjectBasicInfoNumApp(projectBasicInfo));
	}

	/**
	 * 通过id查询项目基本信息表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('oa_projectBasicInfo_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(projectBasicInfoService.getProjectInfoById(id));
	}

	/**
	 * 通过id查询项目基本信息表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/app/{id}")
	public R getByIdApp(@PathVariable("id") Long id) {
		return R.ok(projectBasicInfoService.getProjectInfoById(id));
	}

	/**
	 * 通过id查询项目基本信息表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/getNum/{id}")
	public R getNumByIdApp(@PathVariable("id") Long id) {
		return R.ok(projectBasicInfoService.getNumByIdApp(id));
	}

	/**
	 * 项目立项申请
	 * @param projectBasicInfo 项目基本信息表
	 * @return R
	 */
	@Operation(summary = "新增项目基本信息表", description = "新增项目基本信息表")
	@SysLog("新增项目基本信息表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('oa_projectBasicInfo_add')")
	public R addProject(@RequestBody ProjectBasicInfoEntity projectBasicInfo) {
		return projectBasicInfoService.addProject(projectBasicInfo);
	}

	/**
	 * 修改项目基本信息表
	 * @param projectBasicInfo 项目基本信息表
	 * @return R
	 */
	@Operation(summary = "修改项目基本信息表", description = "修改项目基本信息表")
	@SysLog("修改项目基本信息表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('oa_projectBasicInfo_edit')")
	public R updateById(@RequestBody ProjectBasicInfoEntity projectBasicInfo) {
		return R.ok(projectBasicInfoService.updateById(projectBasicInfo));
	}

	/**
	 * 通过id删除项目基本信息表
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除项目基本信息表", description = "通过id删除项目基本信息表")
	@SysLog("通过id删除项目基本信息表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('oa_projectBasicInfo_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(projectBasicInfoService.removeBatchByIds(CollUtil.toList(ids)));
	}

	/**
	 * 导出excel 表格
	 * @param projectBasicInfo 查询条件
	 * @param ids 导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('oa_projectBasicInfo_export')")
	public List<ProjectBasicInfoEntity> export(ProjectBasicInfoEntity projectBasicInfo, Long[] ids) {
		return projectBasicInfoService.list(Wrappers.lambdaQuery(projectBasicInfo)
			.in(ArrayUtil.isNotEmpty(ids), ProjectBasicInfoEntity::getId, ids));
	}

	/**
	 * 导出excel 表格
	 * @param projectBasicInfo 查询条件
	 * @return excel 文件流
	 */
	@GetMapping("/exportAll")
	public void export(HttpServletResponse response, @ParameterObject ProjectBasicInfoEntity projectBasicInfo) {
		projectBasicInfoService.queryProjectInfoAll(response, projectBasicInfo);
	}

	@SysLog("通过id更新项目状态")
	@PostMapping("/updateProjectStatusById")
	@PreAuthorize("@pms.hasPermission('oa_projectBasicInfo_edit')")
	public R updateProjectStatusById(@Param("id") String id, @Param("status") String status) {
		return R.ok(projectBasicInfoService.updateProjectStatusById(id, status));
	}

}
